package org.ceteca.explica.core.common;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.installer.FieldOT;
import org.ceteca.explica.core.util.DateTime;
import org.ceteca.explica.core.util.FieldValue;

public abstract class BaseVO{

	
	public String[] format(String fieldId, ArrayList<FieldOT> lFields) {
		if (lFields == null || lFields.size() <= 0) {
			if (fieldId == null || fieldId.length() <= 0)
				return null;
			else {
				String[] result = new String[1];
				result[0] = this.formatValue(fieldId);
				
				return result;
			} // else (fieldId != null)
		} // if (lFields == null)
		else {
			int numColumns = 0;
			int firstColumn = 0;
			if (fieldId != null && fieldId.length() > 0) {
				numColumns++;
				firstColumn++;
			}
			
			numColumns += this.getVisibleFieldsCount(lFields);
			String[] result = new String[numColumns];
			int currColumn = firstColumn;
			// Format all the fields included in the formatted vo output
			for (int i=0; i<lFields.size(); i++) {
				if (lFields.get(i).isVisible()) {
					result[currColumn] = this.formatValue(lFields.get(i).getId());
					currColumn++;
				}
			} // for each field included in the formatted vo output
			
			// Format the identifier field
			if (firstColumn > 0)
				result[0] = this.formatValue(fieldId);
		
			return result;
		} // else (lFields != null)
	}
	
	public String formatValue(String fieldId) {
		String value = null;
		if (fieldId != null && fieldId.length() > 0) {
			value = FieldValue.parseFieldValue(this, fieldId);
			System.out.println("Formatting field " + fieldId + ": " + value);
		}
		
		return value;
	}
	
	public abstract boolean equals(Long id);
	public boolean equals(Object obj){
		return super.equals(obj);
	}
	public abstract long getInternalId();
	public abstract String getInternalValue();
	public abstract String getEntityId();
	
	
	
	/**
	 * Calculates the number of visible fields of the entity.
	 * @return int, visible fields number; 1 in case no visible fields
	 * have been defined.
	 */
	public int getVisibleFieldsCount(ArrayList<FieldOT> lFields) {
		int numFields = 0;
		if (lFields != null && lFields.size() > 0) {
			for (FieldOT field : lFields) {
				if (field.isVisible())
					numFields++;
			}
		}
		
		return numFields==0?1:numFields;
	}
}
